package com.kingyun.gpsinspection.purificationservice.services.som.innerservice.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.enums.RespCodeEnum;
import com.kingyun.gpsinspection.purificationservice.common.constant.BackMessage;
import com.kingyun.gpsinspection.purificationservice.common.utils.BootstrapUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.AdmittanceApprMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.AdmittanceMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.AdmittanceOrgMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.Admittance;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.AdmittanceAppr;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.AdmittanceOrg;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.SomAdmittanceService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * 准入申请接口实现类
 * Created by yangcs on 2017/3/27.
 */
public class SomAdmittanceServiceImpl implements SomAdmittanceService {

    @Autowired
    private AdmittanceMapper admittanceMapper;
    @Autowired
    private AdmittanceApprMapper admittanceApprMapper;
    @Autowired
    private AdmittanceOrgMapper admittanceOrgMapper;

    /**
     * 准入申请列表数据
     *
     * @param admittance
     * @return
     */
    @Override
    public JSONObject queryList(Admittance admittance) {
        JSONObject jsonObject = new JSONObject();
        admittance.setOffset(admittance.getOffset() == null ? 0 : admittance.getOffset());
        admittance.setLimit(admittance.getLimit() == null ? 10 : admittance.getLimit());
        jsonObject.put("rows", JSON.toJSON(admittanceMapper.queryList(admittance)));
        jsonObject.put("total", admittanceMapper.queryCount(admittance));
        return jsonObject;
    }

    /**
     * 批量删除
     *
     * @param list
     * @param userId
     * @return
     */
    @Override
    public Map<String, Object> delRepItem(List<String> list, String userId) {
        Map<String, Object> resMap = new HashMap<>();
        Admittance admittance = new Admittance();
        admittance.setModifyUserId(userId);
        admittance.setStatus(6);
        if (list != null) {
            for (String itemId : list) {
                admittance.setAdmittanceId(itemId);
                admittanceMapper.updateDelStatus(admittance);
            }
            resMap.put("msg", BackMessage.DEL_OK);
            resMap.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
        } else {
            resMap.put("msg", BackMessage.EXCEPTION_ISNULL);
            resMap.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
        }
        return resMap;
    }

    /**
     * 新增准入申请
     *
     * @param admittance
     * @return
     */
    @Override
    public int saveAdmittance(Admittance admittance) {
        List<String> orgs = Arrays.asList(admittance.getInOrgs().split(","));
        insertAdmittanceOrg(orgs, admittance);
        return admittanceMapper.insert(admittance);
    }

    /**
     * 修改准入信息
     *
     * @param admittance
     * @return
     */
    @Override
    public int updateAdmittance(Admittance admittance) {
        // 先删除准入信息与组织的关联
        admittanceOrgMapper.deleteByAdmittanceId(admittance.getAdmittanceId());
        // 插入新的准入信息和组织的关联
        List<String> orgs = Arrays.asList(admittance.getInOrgs().split(","));
        insertAdmittanceOrg(orgs, admittance);
        return admittanceMapper.updateAdmittance(admittance);
    }

    /**
     * 根据主键查询准入信息
     *
     * @return
     */
    @Override
    public Admittance queryById(String admittanceId) {
        return admittanceMapper.queryById(admittanceId);
    }

    /**
     * 查询审批信息
     *
     * @param admittanceId
     * @param status
     * @return
     */
    @Override
    public AdmittanceAppr queryByIdAndStatus(String admittanceId, int status) {
        Map map = new HashMap<>();
        map.put("admittanceId", admittanceId);
        map.put("status", status);
        return admittanceApprMapper.queryByIdAndStatus(map);
    }

    /**
     * 插入审核信息
     *
     * @param admittanceAppr
     * @return
     */
    @Override
    public int insertAdmittanceAppr(AdmittanceAppr admittanceAppr) {
        // 当审核状态为主管领导审批通过时生成验证码
        Map map = new HashMap<>();
        map.put("admittanceId", admittanceAppr.getAdmittanceId());
        map.put("status", admittanceAppr.getAfterStatus());
        if (admittanceAppr.getAfterStatus() == 4) {
            String authCode = getAuthCode();
            map.put("authCode", authCode);
            admittanceMapper.updateAuthCode(map);
        }
        admittanceMapper.updateStatusById(map);
        admittanceAppr.setApprId(UniqueUtil.uuid());
        return admittanceApprMapper.insert(admittanceAppr);
    }

    /**
     * 插入关联信息
     *
     * @param orgs
     * @param admittance
     */
    private void insertAdmittanceOrg(List<String> orgs, Admittance admittance) {
        List<AdmittanceOrg> admittanceOrgs = new ArrayList<>();
        AdmittanceOrg admittanceOrg;
        if (orgs != null && orgs.size() > 0) {
            for (String orgId : orgs) {
                admittanceOrg = new AdmittanceOrg();
                admittanceOrg.setOrgId(orgId);
                admittanceOrg.setAdmittanceId(admittance.getAdmittanceId());
                admittanceOrg.setAdmittanceOrgId(UniqueUtil.uuid());
                admittanceOrgs.add(admittanceOrg);
            }
        }
        admittanceMapper.insertOrgIn(admittanceOrgs);
    }

    /**
     * 生成进出站验证码
     *
     * @return
     */
    private String getAuthCode() {
        String code = BootstrapUtil.getAuthCode();
        List<Admittance> list = admittanceMapper.queryByAuthCode(code);
        if (list != null && list.size() > 0) {
            code = getAuthCode();
        }
        return code;
    }

}
